package com.zirenx.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.jfinal.aop.Inject;
import com.zirenx.annotation.RouteMapping;
import com.zirenx.common.controller.BaseController;
import com.zirenx.common.controller.IController;
import com.zirenx.sys.model.SysDept;
import com.zirenx.sys.service.SysDeptService;
import com.zirenx.utils.LogUtils;
import com.zirenx.utils.R;
import com.zirenx.utils.StringUtils;
import com.zirenx.utils.tree.Tree;

@RouteMapping("/sys/dept")
public class SysDeptController extends BaseController implements IController{
	@Inject
	SysDeptService sysDeptService;
	
	public void index() {
		render("index.html");
	}
	
	public void add() {
		String parentId = get("parentId");
		SysDept parent = new SysDept();
		parent.setId(0l);
		parent.setName("顶级菜单");
		parent.setType(new Integer(SysDept.TypeEnum.机构.getValue()));
		if(!StringUtils.isEmpty(parentId) && !parentId.equals("0")) {
			SysDept bean = sysDeptService.findById(parentId);
			if(bean!=null) {
				parent = bean;
				if(parent.getType()!=null && parent.getType().intValue() < SysDept.TypeEnum.岗位.getValue()) {
					parent.setType(parent.getType().intValue()+1);
				}
			}
		}
		
		setAttr("parent", parent);
		setAttr("bean", new SysDept());
		setAttr("typeList", SysDept.TypeEnum.getAll());
		render("add.html");
	}
	
	public void edit() {
		SysDept bean = sysDeptService.findById(get("id"));
		Long parentId = bean.getParentId();
		String parentName = "顶级菜单";
		if(parentId!=null && parentId.longValue()>0) {
			SysDept parent = sysDeptService.findById(parentId+"");
			parentName = parent.getName();
		}
		setAttr("bean", bean);
		setAttr("parentName", parentName);
		setAttr("typeList", SysDept.TypeEnum.getAll());
		render("edit.html");
	}
	
	public void save() {
		R r = R.ok();
		try {
			SysDept bean = getBean(SysDept.class,"bean");
			bean.setGmtCreate(new Date());
			bean.save();
		} catch (Exception e) {
			e.printStackTrace();
			r = R.error();
		}
		renderJson(r);
	}
	
	public void update() {
		R r = R.ok();
		try {
			SysDept bean = getBean(SysDept.class,"bean");
			bean.setGmtModified(new Date());
			bean.update();
		} catch (Exception e) {
			e.printStackTrace();
			r = R.error();
		}
		renderJson(r);
	}
	
	public void remove() {
		R r = R.ok();
		try {
			sysDeptService.delete(getPara());
			renderJson(R.ok());
		} catch (Exception e) {
			e.printStackTrace();
			r = R.error();
		}
		renderJson(r);
	}
	
	public void dataAjax() {
		List<SysDept> list = new ArrayList<>();
		try {
			list = sysDeptService.list(new HashMap<>());
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
		}
		renderJson(list);
	}
	
	public void batchRemove() {
		R r = R.ok();
		try {
			sysDeptService.batchRemove(getParaValues("ids"));
			renderJson(R.ok());
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
			r = R.error();
		}
		renderJson(r);
	}
	
	public void tree() {
		R r = R.ok();
		try {
			Tree<SysDept> tree = sysDeptService.tree();
			renderJson(tree);
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
			r = R.error(); 
			renderJson(r);
		}
	}

	/**
	 * 树结构视图
	 */
	public void treeView(){

	}
}
